package com.group9.controller;

import com.group9.pojo.CustomerInfo;
import com.group9.service.DoubleNameService;
import com.group9.service.UserLoginService;
import com.group9.service.UserLogonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by 45865 on 2020/5/6.
 */
@Controller
@RequestMapping("userinfo")
@SessionAttributes(names = {"loginName"})
public class UserLoginController {
    //接收前端输入的用户名和密码 然后进行验证，验证通过 将数据保存在session中
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    UserLogonService userLogonService;
    @Autowired
    DoubleNameService doubleNameService;


    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody

    //登录功能
//    public String login(Model model,String loginName,String pwd){
     public String login(String loginName,String pwd,Integer a,HttpServletResponse response,HttpServletRequest request,HttpSession session){


       CustomerInfo customerInfo =  userLoginService.selectByLoginNameAndPwd(loginName,pwd);
        if (null != customerInfo){
            //将loginName存入session中
            session.setAttribute("loginName",loginName);
            session.setAttribute("customerId",customerInfo.getCustomerId());
            //success
//            model.addAttribute("loginName",customerInfo.getLoginName());
            //记住密码 存用户名
            if (a == 1) {
                Cookie usercookie = new Cookie("loginName", loginName);
                //设置有效期一周
                usercookie.setMaxAge(7 * 24 * 60 * 60);
                Cookie pwdcookie = new Cookie("pwd", pwd);
                pwdcookie.setMaxAge(7 * 24 * 60 * 60);
                response.addCookie(usercookie);
                response.addCookie(pwdcookie);
            }

            return "success";
        }else {
            //fail
            return "fail";
        }
    }

    //注册功能
    @RequestMapping(value = "logon",method = RequestMethod.POST)
    @ResponseBody
    public int logon(String userName,String pwd,String phone) {

        int affectRows = userLogonService.inserUserInfo(userName, pwd, phone);
        System.out.println(userName);
        System.out.println(pwd);
        System.out.println(phone);
        if (affectRows == 1) {
            return 1;
        } else {
            return 0;
        }
    }

    @RequestMapping(value = "double",method = RequestMethod.POST)
    @ResponseBody
    public String namedouble(String userName){
        System.out.println("==========================");
        System.out.println(userName);
        System.out.println("==========================");
         CustomerInfo customerInfo = doubleNameService.doubleName(userName);
        System.out.println(customerInfo);
         if (customerInfo != null){
                 return "fail";
         }else {
             return "success";
         }
    }
}
